/**
 * 用户详情对话框
 */
var UserInfoDlg = {
    data: {
        deptId: "",
        deptName: ""
    }
};
var areaNames="";

layui.use(['layer', 'form', 'admin', 'laydate', 'ax','citypicker'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var layer = layui.layer;
    var cityPicker = layui.citypicker;

    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    //获取用户信息
    var ajax = new $ax(Feng.ctxPath + "/mgr/getUserInfo?userId=" + Feng.getUrlParam("userId"));
    var result = ajax.start();
    var currentPicker = new cityPicker("#area", {
        provincename:"广东省",
        cityname:"东莞市",
        districtname: "寮步镇",
        level: '3',// 级别
    });
    var areaData = result.data.area;
    var areaDatas = areaData.split(",");
    console.log("区域："+areaDatas[0]+","+areaDatas.length);
    currentPicker.setValue(areaDatas[0]);
    if(areaDatas.length>1){
        $("#areaNext").val(areaDatas[1]);
    }
    form.val('userForm', result.data);
    $("#area").val(areaDatas[0]);

    var areaNextChange = function(){
        var curAreaName = $("#area").val();
        if(curAreaName!=areaNames){
            var that = $("#areaNext")[0];
            $.getJSON(Feng.ctxPath+"/region/searchReturnCode?params="+$("#area").val()+"&r="+Math.random(),function(areaMap){
                $(that).empty();
                var isFirst = true;
                for(area in areaMap){
                    if(isFirst) {
                        $("#areaCode").val(area);
                        isFirst = false;
                    }
                    $(that).html($(that).html()+"<option value='"+area+"'>"+areaMap[area]+"</option>");
                }
                form.render("select");
                layer.closeAll();
                $("#areaNext").parent().find(":text").click(function(){
                    areaNextChange();
                })
            });
            areaNames = curAreaName;
        }
    }
    $("#areaNext").parent().find(":text").click(function(){
        layer.msg('加载村数据中', {
            icon: 16
            ,shade: 0.01
        });
        areaNextChange();
    })
    if($("#area").val()!="" && result.data.areaCode.length>6){
        $("#areaNext").parent().find(":text").click();
    }
    form.on("select(areaFilter)",function (data) {
        console.table(data);
        $("#areaCode").val(data.value);
    })

    // 点击部门时
    $('#deptName').click(function () {
        var formName = encodeURIComponent("parent.UserInfoDlg.data.deptName");
        var formId = encodeURIComponent("parent.UserInfoDlg.data.deptId");
        var treeUrl = encodeURIComponent("/dept/tree");

        layer.open({
            type: 2,
            title: '部门选择',
            area: ['300px', '400px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                console.log(UserInfoDlg.data);
                $("#deptId").val(UserInfoDlg.data.deptId);
                $("#deptName").val(UserInfoDlg.data.deptName);
            }
        });
    });

    // 添加表单验证方法
    form.verify({
        psw: [/^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'],
        repsw: function (value) {
            if (value !== $('#userForm input[name=password]').val()) {
                return '两次密码输入不一致';
            }
        }
    });

    // 渲染时间选择框
    laydate.render({
        elem: '#birthday'
    });

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/mgr/edit", function (data) {
            if(data.success){
                Feng.success("修改成功！");

                //传给上个页面，刷新table用
                admin.putTempData('formOk', true);

                //关掉对话框
                admin.closeThisDialog();
            }else{
                Feng.error("修改失败！"+data.message);
            }

        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
    });
});